package com.costaccounting.base.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.costaccounting.base.domain.ZsCategory;
import com.costaccounting.base.domain.ZsCategoryPersonConfig;
import com.costaccounting.base.domain.ZsItemCategoryConfig;
import com.costaccounting.base.service.IZsCategoryPersonConfigService;
import com.costaccounting.base.service.IZsCategoryService;
import com.costaccounting.base.service.IZsItemCategoryConfigService;
import com.costaccounting.common.core.domain.AjaxResult;
import com.costaccounting.common.core.domain.entity.SysDictData;
import com.costaccounting.common.core.domain.entity.SysUser;
import com.costaccounting.common.utils.DateUtils;
import com.costaccounting.common.utils.SecurityUtils;
import com.costaccounting.system.mapper.SysUserMapper;
import com.costaccounting.system.service.ISysDictDataService;
import com.costaccounting.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.costaccounting.base.mapper.ZsItemMapper;
import com.costaccounting.base.domain.ZsItem;
import com.costaccounting.base.service.IZsItemService;

/**
 * 项目管理Service业务层处理
 *
 * @author H
 * @date 2021-11-06
 */
@Service
public class ZsItemServiceImpl implements IZsItemService {
    @Autowired
    private ZsItemMapper zsItemMapper;
    @Autowired
    private ISysDictDataService dictDataService;
    @Autowired
    private IZsCategoryService categoryService;
    @Autowired
    private ISysUserService userService;
    @Autowired
    private IZsItemCategoryConfigService itemCategoryConfigService;


    /**
     * 查询项目管理
     *
     * @param id 项目管理主键
     * @return 项目管理
     */
    @Override
    public ZsItem selectZsItemById(Long id) {
        return zsItemMapper.selectZsItemById(id);
    }

    /**
     * 查询项目管理列表
     *
     * @param zsItem 项目管理
     * @return 项目管理
     */
    @Override
    public List<ZsItem> selectZsItemList(ZsItem zsItem) {
        SysUser sysUser = userService.selectUserById(SecurityUtils.getUserId());

        if("102".equals(sysUser.getRoleType().toString())){
            zsItem.setLeaderCode(sysUser.getUserName());
        }
        return zsItemMapper.selectZsItemList(zsItem);
    }

    /**
     * 新增项目管理
     *
     * @param zsItem 项目管理
     * @return 结果
     */
    @Override
    public int insertZsItem(ZsItem zsItem) {
        zsItem.setCreateTime(DateUtils.getNowDate());
        zsItem.setCategoryList(zsItem.getCategoryIdList().toString());
        zsItem.setItemStatus("0");
        return zsItemMapper.insertZsItem(zsItem);
    }

    /**
     * 修改项目管理
     *
     * @param zsItem 项目管理
     * @return 结果
     */
    @Override
    public int updateZsItem(ZsItem zsItem) {
        String categoryIds = "";
        String buildingIds = "";
        List<List<Long>> categoryIdList = zsItem.getCategoryIdList();
        if (null != categoryIdList && 0 < categoryIdList.size()) {
            for (int i = 0; i < categoryIdList.size(); i++) {
                List<Long> idList = categoryIdList.get(i);
                if (0 == i) {
                    categoryIds = idList.get(idList.size() - 1).toString();
                } else {
                    categoryIds = categoryIds + "," + idList.get(idList.size() - 1).toString();
                }
            }
        }

        ZsItemCategoryConfig itemCategoryConfig=new ZsItemCategoryConfig();
        itemCategoryConfig.setItemId(zsItem.getId());
        List<ZsItemCategoryConfig> zsItemCategoryConfigs = itemCategoryConfigService.selectZsItemCategoryConfigList(itemCategoryConfig);
        List<Long> ids=new ArrayList<>();
        if(null!=zsItemCategoryConfigs&&0<zsItemCategoryConfigs.size()){
            for (ZsItemCategoryConfig zsItemCategoryConfig : zsItemCategoryConfigs) {
                ids.add(zsItemCategoryConfig.getId());
            }
            itemCategoryConfigService.deleteZsItemCategoryConfigByIds(ids.toArray(new Long[0]));
        }

        List<ZsItemCategoryConfig> buildingListData = zsItem.getBuildingListData();
        if (null != buildingListData && 0 < buildingListData.size()) {
            for (int i = 0; i < buildingListData.size(); i++) {
                ZsItemCategoryConfig zsItemCategoryConfig = buildingListData.get(i);
                Long buildingId = zsItemCategoryConfig.getCategoryId();
                if (0 == i) {
                    buildingIds = buildingId.toString();
                } else {
                    buildingIds = buildingIds + "," + buildingId.toString();
                }
                zsItemCategoryConfig.setDelFlag("0");
                zsItemCategoryConfig.setUpdateBy(SecurityUtils.getUsername());
                zsItemCategoryConfig.setUpdateTime(new Date());
                itemCategoryConfigService.insertZsItemCategoryConfig(zsItemCategoryConfig);
            }
        }

        zsItem.setCategoryIds(categoryIds);
        zsItem.setBuildingIds(buildingIds);
        zsItem.setCategoryList(zsItem.getCategoryIdList().toString());
        zsItem.setUpdateTime(DateUtils.getNowDate());
        return zsItemMapper.updateZsItem(zsItem);
    }

    /**
     * 批量删除项目管理
     *
     * @param ids 需要删除的项目管理主键
     * @return 结果
     */
    @Override
    public int deleteZsItemByIds(Long[] ids) {
        return zsItemMapper.deleteZsItemByIds(ids);
    }

    /**
     * 删除项目管理信息
     *
     * @param id 项目管理主键
     * @return 结果
     */
    @Override
    public int deleteZsItemById(Long id) {
        return zsItemMapper.deleteZsItemById(id);
    }

    @Override
    public AjaxResult getInfo(ZsItem zsItem) {
        AjaxResult ajax = AjaxResult.success();

        //楼号
        zsItem = zsItemMapper.selectZsItemById(zsItem.getId());
        SysDictData dictData = new SysDictData();
        dictData.setDictType("building_no");
        dictData.setStatus("0");
        List<SysDictData> buildingOptions = dictDataService.selectDictDataList(dictData);
        ajax.put("buildingOptions", buildingOptions);

        //类目
        ZsCategoryPersonConfig categoryPersonConfig=new ZsCategoryPersonConfig();
        categoryPersonConfig.setItemId(zsItem.getId());
        categoryPersonConfig.setItemName(zsItem.getName());
        List<ZsCategory> noList = categoryService.getNoList(categoryPersonConfig);


        ajax.put("categoryOptions", noList);
        ajax.put("buildingIdList",new ArrayList<>());
        ajax.put("categoryIdList",new ArrayList<>());
        ajax.put(AjaxResult.DATA_TAG, categoryPersonConfig);
        return ajax;
    }
}
